﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Story.Library
{
    /// <summary>
    /// 包含了与字符从操作相关的Helper公共函数
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// 从如下格式的字符串提取特定Key的Value
        /// key1=value1;key2=value2;
        /// </summary>
        /// <param name="key">Key的字符串值</param>
        /// <param name="wholeString">键值对字符串</param>
        /// <returns>对于key的值</returns>
        public static string GetValueFromString(string key, string wholeString)
        {
            if (wholeString == null || wholeString == "") return null;
            string[] key_valueArray = wholeString.Split(';');
            foreach (string key_value in key_valueArray)
            {
                if (!string.IsNullOrEmpty(key_value))
                {
                    string[] tmp = key_value.Split('=');
                    if (tmp.Length > 1 && key.Trim() == tmp[0].Trim())
                        return tmp[1].Trim();
                }
            }
            return null;
        }

        /// <summary>
        /// 从如下格式的字符串提取一个Key数组的Value
        /// key1=value1;key2=value2;
        /// </summary>
        /// <param name="keys">key的数值</param>
        /// <param name="wholeString">键值对字符串</param>
        /// <returns>包含了健值对的泛型字典对象</returns>
        public static Dictionary<string, string> GetValueFromString(string[] keys, string wholeString)
        {
            if (wholeString == null || wholeString == "") return null;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] key_valueArray = wholeString.Split(';');
            foreach (string key_value in key_valueArray)
            {
                if (!string.IsNullOrEmpty(key_value))
                {
                    string[] tmp = key_value.Split('=');
                    if (tmp.Length > 1)
                    {
                        foreach (string key in keys)
                        {
                            if (key.Trim() == tmp[0].Trim())
                            {
                                dic.Add(tmp[0].Trim(), tmp[1].Trim());
                                break;
                            }
                        }
                    }
                }
            }
            return dic;
        }

        /// <summary>
        /// 从如下格式的字符串提取所有键值对
        /// key1=value1;key2=value2;
        /// </summary>
        /// <param name="wholeString">键值对字符串</param>
        /// <returns>包含了健值对的泛型字典对象</returns>
        public static Dictionary<string, string> GetKeyValuePairString(string wholeString)
        {
            if (wholeString == null || wholeString == "") return null;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] key_valueArray = wholeString.Split(';');
            foreach (string key_value in key_valueArray)
            {
                if (!string.IsNullOrEmpty(key_value))
                {
                    string[] tmp = key_value.Split('=');
                    if (tmp.Length > 1)
                    {
                        dic.Add(tmp[0].Trim(), tmp[1].Trim());
                    }
                }
            }
            return dic;
        }
    }
}

